package com.example.demo;

import com.example.demo.mapper.*;
import com.example.demo.pojo.Unit.QuestionNumber;
import com.example.demo.pojo.entity.*;
import com.example.demo.pojo.vo.QuestionVO;
import com.example.demo.utils.ImportQuestion;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
@RequiredArgsConstructor
public class createTabel {
    @Autowired
    private QuestionBankMapper questionBankMapper;
    @Autowired
    private QuestionCategoryMapper questionCategoryMapper;
    @Autowired
    private QuestionClassificationMapper questionClassificationMapper;
    @Autowired
    private QuestionLabelMapper questionLabelMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private QuestionFileMapper questionFileMapper;
    //以文件的形式读取题目到数据库
//    @Test
//    void createJuanZI(){
//        ImportQuestion temp=new ImportQuestion("C:\\Users\\LEGION\\Desktop\\demo.xls");
//        List<String[]> questionList = temp.getQuestionList();
//        List<QuestionBank>questionBankList = new ArrayList<>();
//        if(questionList!=null){
//            System.out.println("this is");
//            questionList.remove(0);
//            for(String[] question : questionList){
//                if(question.length<1){
//                    break;
//                }
//                QuestionBank questionBank=new QuestionBank();
//                String tempCategory=question[0];
//                if(tempCategory==null){
//                    continue;
//                }
//                Integer categoryId=questionCategoryMapper.searchCategory(tempCategory);
//                String questionName=question[1];
//                String parse=question[3];
//                String htmlName=ImportQuestion.formatJavaClass(questionName);
//                String htmlParse=ImportQuestion.formatJavaClass(parse);
//                questionBank.setCategory(categoryId);
//                questionBank.setName(questionName); //名字
//                questionBank.setCorrectAnswer(question[2]);
//                questionBank.setParse(question[3]); //解析
//                questionBank.setDifficulty(question[4]);
//                questionBank.setHtmlName(htmlName);
//                questionBank.setHtmlParse(htmlParse);
//                //处理题型
//                String tempLabelName;
//                try {
//                    tempLabelName=question[5];
//                }catch (Exception e){
//                    questionBankList.add(questionBank);
//                    continue;
//                }
//                questionBank.setLabel(tempLabelName);
//                double answer_number;
//                StringBuilder option= new StringBuilder();
//                StringBuilder htmlOption=new StringBuilder();
//                try {
//                    answer_number=Double.parseDouble(question[6]);
//                    questionBank.setAnswerNumber(answer_number);
//                    for(int i=7;i<7+answer_number;i++){
//                        try
//                        {
//                            String oneOption=question[i];
//                            if (oneOption.contains("\n")) {
//                                break;
//                            }
//                            option.append(oneOption);
//                            htmlOption.append(ImportQuestion.formatJavaClass(oneOption));
//                            htmlOption.append('|');
//                            option.append('|');
//                        }
//                        catch (Exception e)
//                        {
//                            break;
//                        }
//                    }
//                }catch (Exception e){
//                    questionBank.setOptions(String.valueOf(option));
//                    questionBankList.add(questionBank);
//                    continue;
//                }
//                questionBank.setOptions(String.valueOf(option));
//                questionBank.setHtmlOption(String.valueOf(htmlOption));
//                questionBankList.add(questionBank);
//            }
//            //for循环结束,将列表中的题目添加到数据库中
//            for(int i=0;i<questionBankList.size();i++){
//                QuestionBank tempBank=questionBankList.get(i);
//                questionBankMapper.insert(tempBank);
//            }
//        }
//    }
    //建立关联表
    @Test
    void makeQuestionClassification(){
        List<QuestionBank>questionBankList=questionBankMapper.initQuestionBank();
        for(int i=0;i<questionBankList.size();i++){
            QuestionBank question=questionBankList.get(i);
            String labelName=question.getLabel();
            if(labelName!=null){
                String[] labelNames = labelName.split(";");
                for(int j=0;j<labelNames.length;j++) {
                    List<QuestionLabel> questionLabelList = questionLabelMapper.searchQuestionLabel(labelNames[j]);
                    QuestionLabel questionLabel;
                    System.out.println(question);
                    //题型存在才进行下一步操作
                    if (!questionLabelList.isEmpty()) {
                        questionLabel = questionLabelList.get(0);
                        //获得物化表相关信息
                        int questionLabelId = questionLabel.getId();
                        String nodeId = questionLabel.getNodeId();
                        int questionBankId = question.getId();
                        QuestionClassification questionClassification = new QuestionClassification();
                        questionClassification.setQuestionBankId(questionBankId);
                        questionClassification.setQuestionLabelId(questionLabelId);
                        questionClassification.setNodeId(nodeId);
                        questionClassificationMapper.insert(questionClassification);
                    }
                }
            }else{
                //增添学科处理
                QuestionClassification questionClassification = new QuestionClassification();
                questionClassification.setQuestionBankId(question.getId());
                questionClassification.setQuestionLabelId(1);
                questionClassification.setNodeId("1-");
                questionClassificationMapper.insert(questionClassification);
            }
        }
    }
//    @Test
//    void juanzi() {
//        HashMap<QuestionLabel, Integer> temp = new HashMap<>();
//        List<QuestionBank>questionBankList=new ArrayList<>();
//        List<QuestionNumber>questionNumberList=new ArrayList<>();
//        List<Integer>alreadyQuestionBankIdList=new ArrayList<>();
//        List<Integer>allQuestionBankIdList=new ArrayList<>();
//        //模拟数据.
//        //1.需要抽取的题目
//        temp.put(questionLabelMapper.searchByName("绪论").get(0),20);
//        temp.put(questionLabelMapper.searchByName("JDK与JVM").get(0),4);
//        temp.put(questionLabelMapper.searchByName("编程规范").get(0),5);
//        temp.put(questionLabelMapper.searchByName("变量与常量").get(0),3);
//        //2.需要抽取题目数目
//        int totalCount=32;
//        //过滤数据
//        //1.构建数目表,同时构建排序表
//        for(QuestionLabel questionLabel: temp.keySet()){
//            QuestionNumber tempQuestionNumber = new QuestionNumber(questionLabel, temp.get(questionLabel));
//            questionNumberList.add(tempQuestionNumber);
//        }
//        //2:对排序表进行排序--冒泡排序
//        for(int i=0;i<questionNumberList.size()-1;i++){
//            int maxIndex=i;
//            for(int j=i+1;j<questionNumberList.size();j++){
//                if(questionNumberList.get(j).getLevel()>questionNumberList.get(maxIndex).getLevel()){
//                    maxIndex=j;
//                }
//                    QuestionNumber tempLabel=questionNumberList.get(i);
//                    questionNumberList.set(i,questionNumberList.get(maxIndex));
//                    questionNumberList.set(maxIndex,tempLabel);
//            }
//        }
//        //选取题目
//        //1.根据level进行高到低的查找题目
//        for(int i=questionNumberList.size()-1;i>=0;i--){
//            //获得当前的
//            QuestionNumber questionNumber=questionNumberList.get(i);
//            while (questionNumber.getNumber()>0){
//                int labelId=questionNumber.getQuestionLabelId();
//            }
//        }
//        //测试
//        for(int i=0;i<questionNumberList.size();i++){
//            System.out.println(questionNumberList.get(i));
//        }
//    }
    //统计出各知识点下的题目数量
    @Test
    void getNumberBank(){
//        List<QuestionLabel>questionLabelList=questionLabelMapper.initQuestionLabel(1);
//        List<QuestionVO>questionClassificationList=initQuestionClassification();
//        List<LabelCountVO>labelCountList=new ArrayList<>();
//        HashMap<Integer,LabelCountVO>countMap=new HashMap<>();
//        //选择数量的同时指定题目的类型
//        int tempCategory=1;
//        //初始化HashMap计数
//       for(int i=0; i<questionLabelList.size(); i++){
//           QuestionLabel questionLabel=questionLabelList.get(i);
//           LabelCountVO tempLabelCountVO=new LabelCountVO();
//           tempLabelCountVO.setCount(0);
//           tempLabelCountVO.initLabel(questionLabel);
//           countMap.put(questionLabel.getId(), tempLabelCountVO);
//       }
//       //开始统计个数
//        for(int i=0;i<questionClassificationList.size();i++){
//            QuestionVO questionVO=questionClassificationList.get(i);
//            Integer labelId=questionVO.getLabelId();
//            Integer bankCategory=questionVO.getCategory();
//            //将值从hashMap中取出然后+1
//            if(bankCategory!=null&&countMap.containsKey(labelId)&&bankCategory==tempCategory){
//                LabelCountVO tempValue=countMap.get(labelId);
//                Integer tempCount=tempValue.getCount();
//                tempCount=tempCount+1;
//                tempValue.setCount(tempCount);
//                countMap.put(labelId,tempValue);
//            }
//        }
//        //传回后端
//        for (Integer key : countMap.keySet()) {
//            labelCountList.add(countMap.get(key));
//        }
//        for(int i=0;i<labelCountList.size();i++){
//            System.out.println(labelCountList.get(i));
//        }
    }
    //从物化路径中获得所有题目
//    @Test
    List<QuestionVO> initQuestionClassification(){
        List<QuestionVO>questionVOList=new ArrayList<>();
        List<QuestionClassification>questionClassificationList=questionClassificationMapper.initQuestionClassificationByRootId(1);
        for(int i=0;i<questionClassificationList.size();i++){
            QuestionVO questionVO=new QuestionVO();
            QuestionBank questionBank=questionBankMapper.selectById(questionClassificationList.get(i).getQuestionBankId());
            QuestionLabel questionLabel=questionLabelMapper.selectById(questionClassificationList.get(i).getQuestionLabelId());
            questionVO.initBank(questionBank);
            questionVO.initLabel(questionLabel);
            Integer categoryId=questionBank.getCategory();
            QuestionCategory questionCategory=questionCategoryMapper.selectById(categoryId);
            if(questionCategory!=null){
                questionVO.setCategoryName(questionCategory.getName());
            }
            questionVOList.add(questionVO);
        }
        return questionVOList;
    }
    //建立文件表
    @Test
    void createFile(){
        Integer subjectId=1;//模拟传入的参数--选中java
        Subject subject=subjectMapper.selectById(subjectId);

        System.out.println(subject);
        Integer fileId=subject.getFileId();//获取根目录的id
//        File file=fileMapper.selectById(fileId);
        List<QuestionBank>questionBankList=questionBankMapper.initQuestionBank();
        for(int i=0;i<questionBankList.size();i++){
            QuestionFile questionFile=new QuestionFile();
            questionFile.setFileId(fileId);
            QuestionBank questionBank=questionBankList.get(i);
            questionFile.setQuestionId(questionBank.getId());
//            questionFile.setNodeId(file.getNodeId());
            questionFileMapper.insert(questionFile);
        }
    }
    @Test
    void test(){
        List<Integer> question=questionClassificationMapper.searchLabelIdListByBankId(16896);
        System.out.println(question);
    }
}
